package com.hailin.compress;

import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.hailin.compress.annotation.SPI;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

/**
 * @Description   
 * @Author hailin
 * @Date 2018/6/22 11:39  
 * @Version 1.0
 */  
public enum  CompressFactory {

   CF;

   private  Map<String, Compress> compressMap = new HashMap<String, Compress>();

   CompressFactory(){
     ServiceLoader<Compress> compresses = ServiceLoader.load(Compress.class);
     compresses.iterator().forEachRemaining(compress -> compressMap.put(compress.getClass().getAnnotation(SPI.class).value() , compress));
   }

    /**
     * 根据注解名获取Compess
     * @param name
     * @return
     */
   public Compress getCompress(String name){
       return compressMap.get(name);
   }



}
